﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;

namespace DAO
{
    public class TaiKhoanNguoiDungDAO
    {
        public static List<TaiKhoanNguoiDung> LayDS()
        {
            try
            {
                QLSTLinqDataContext rv = new QLSTLinqDataContext();
                List<TaiKhoanNguoiDung> temp = new List<TaiKhoanNguoiDung>();
                var query = from c in rv.TaiKhoanNguoiDungs
                            select c;
                temp = query.ToList();
                return temp;
            }
            catch
            {
                return null;
            }
        }

        public static bool Them(TaiKhoanNguoiDung aCT)
        {
            try
            {
                QLSTLinqDataContext rv = new QLSTLinqDataContext();
                rv.TaiKhoanNguoiDungs.InsertOnSubmit(aCT);
                rv.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool Xoa(int maCT)
        {
            try
            {
                QLSTLinqDataContext rv = new QLSTLinqDataContext();
                var cust =
    (from c in rv.TaiKhoanNguoiDungs
     where c.Id == maCT
     select c).First();

                rv.TaiKhoanNguoiDungs.DeleteOnSubmit(cust);
                rv.SubmitChanges();



                return true;
            }
            catch
            {
                return false;
            }
        }

        public static TaiKhoanNguoiDung DangNhap(string ten, string pass)
        {
            try
            {
                QLSTLinqDataContext rv = new QLSTLinqDataContext();
                var query = from c in rv.TaiKhoanNguoiDungs
                            where c.UserName == ten && c.MatKhau == pass
                            select c;

                if (query.First() == null)
                    return null;
                return query.First();


            }
            catch
            {
                return null;
            }
        }
    }
}
